﻿using FirstView.Entities;
using PublishingHouse.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;

namespace PublishingHouse.Services
{
    public class NeedBook
    {
        public string CustomerName;
        public string BookName;
        public string isbn;
        public string AuthorName;
        public decimal Inventory;
        public string SaleCount;
        public decimal Price;
        public string Type;
    }
    public class DetailBook
    {
        public string CustomerName;
        public string isbn;
        public string content;
        public decimal Inventory;
    }
    public class CustomerOrder
    {
        public string Cname;
        public int Id;
        public string BookName;
        public decimal BookNumber;
        public decimal BookPrice;
        public decimal TotalPrice;
        public string OrderTime;
        public string OrderState;
        public string Address;
    }
    public class CustomerService
    {
        private CustomerRepository customerRepository;
        public CustomerService()
        {
            customerRepository = new CustomerRepository();
        }
        public bool LoginJudge(string id, string password)
        {
            var customer = customerRepository.Query(id);
            if (customer == null)
                return false;
            if (customer.Password == password)
            {
                return true;
            }
            else
                return false;
        }

        public bool RegisterJudge(string Registerid, string password, string phone)
        {
            var customer = customerRepository.Query(Registerid);
            if (customer != null)
                return false;
            if (customer == null)
            {
                customerRepository.Add(Registerid, password, phone);
                return true;
            }
            return false;
        }
        public Customers Query(string id)
        {
            var customer = customerRepository.Query(id);
            return customer;
        }
        public void Update(Customers customer)
        {
            customerRepository.Update(customer);
        }
        public List<NeedBook> NeedbookList(string id)
        {
            string name = customerRepository.Query(id).Name;
            var bookauthor = customerRepository.QueryBookAuthor();
            List<NeedBook> NeedBookList = new List<NeedBook>();
            for(int i = 0; i < bookauthor.Count; i++)
            {
                NeedBook temp = new NeedBook();
                var book = customerRepository.QueryBook(bookauthor[i].Bid);
                var author = customerRepository.QueryAuthor(bookauthor[i].Aid);
                if ((book != null) && (author != null))
                {
                    temp.CustomerName = name;
                    temp.BookName = book.Name;
                    temp.isbn = bookauthor[i].Bid;
                    temp.AuthorName = author.Name;
                    temp.Inventory = book.Inventory;
                    temp.SaleCount = book.Countsale;
                    temp.Price = book.Price;
                    temp.Type = book.Type;
                    NeedBookList.Add(temp);
                }
                else
                    continue;
            }
            return NeedBookList;
        }
        public List<NeedBook> BookTypeList(string id,string booktype)
        {
            string name = customerRepository.Query(id).Name;
            var bookauthor = customerRepository.QueryBookAuthor();
            List<NeedBook> NeedBookList = new List<NeedBook>();
            for (int i = 0; i < bookauthor.Count; i++)
            {
                NeedBook temp = new NeedBook();
                var book = customerRepository.QueryBook(bookauthor[i].Bid);
                var author = customerRepository.QueryAuthor(bookauthor[i].Aid);
                if ((book != null) && (author != null)&&(book.Type==booktype))
                {
                    temp.CustomerName = name;
                    temp.BookName = book.Name;
                    temp.isbn = bookauthor[i].Bid;
                    temp.AuthorName = author.Name;
                    temp.Inventory = book.Inventory;
                    temp.SaleCount = book.Countsale;
                    temp.Price = book.Price;
                    temp.Type = book.Type;
                    NeedBookList.Add(temp);
                }
                else
                    continue;
            }
            return NeedBookList;
        }
        public DetailBook BookDetail(string isbn, string cid)
        {
            DetailBook bookdetail = new DetailBook();
            var book = customerRepository.QueryBook(isbn);
            var customer = customerRepository.Query(cid);
            bookdetail.isbn = isbn;
            bookdetail.content = book.Content;
            bookdetail.Inventory = book.Inventory;
            bookdetail.CustomerName = customer.Name;
            return bookdetail;
        }
        public bool AddOrder(string cid, string isbn, string address,decimal number)
        {
            var book = customerRepository.QueryBook(isbn);
            if (number > book.Inventory)
            {
                return false;
            }
            Order order = new Order
            {
                Cid = cid,
                OrderTime = DateTime.Now.ToLocalTime().ToString(),
                OrderStatus = "等待发货",
                Quantity = number,
                Address = address,
                Bookprice = book.Price,
                TotalPrice = book.Price * number,
                Bookname = book.Name
            };
            customerRepository.AddOrder(order);
            book.Inventory -= number;
            var temp =int.Parse(book.Countsale) + number;
            book.Countsale = temp.ToString();
            customerRepository.UpdateBook(book);
            return true;
        }
        public List<CustomerOrder> OrderList(string cid)
        {
            string name = customerRepository.Query(cid).Name;
            var orderList = customerRepository.QueryOrder(cid);
            List<CustomerOrder> CustomerOrderList = new List<CustomerOrder>();
            if(orderList.Count==0)
            {
                CustomerOrder customerOrder = new CustomerOrder
                {
                    Cname = name
                };
                CustomerOrderList.Add(customerOrder);
            }
            else
            {
                for (int i = 0; i < orderList.Count; i++)
                {
                    CustomerOrder customerOrder = new CustomerOrder
                    {
                        Id = orderList[i].Id,
                        Cname = name,
                        BookName = orderList[i].Bookname,
                        BookNumber = orderList[i].Quantity,
                        BookPrice = orderList[i].Bookprice,
                        TotalPrice = orderList[i].TotalPrice,
                        OrderTime = orderList[i].OrderTime,
                        OrderState = orderList[i].OrderStatus,
                        Address = orderList[i].Address
                    };
                    CustomerOrderList.Add(customerOrder);
                }
            }
            return CustomerOrderList;
        }
        public void ReturnBook(int id)
        {
            var order = customerRepository.QueryReturnOrder(id);
            order.OrderStatus = "退书中";
            customerRepository.UpdateOrder(order);
        }
        public void ReceiveBook(int id)
        {
            var order = customerRepository.QueryReturnOrder(id);
            order.OrderStatus = "已收货";
            customerRepository.UpdateOrder(order);
        }

    }
}
